﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using System.

namespace ProgramTimer
{
    public partial class Form1 : Form
    {

     int Power =8 ;
     int Status = 0;
     int button = 0;
     int delay = 0;
     int delay1 = 0;
     bool DisplayOn=true;
     const byte TimerDisplayOn = 150;
     byte TimerDisplayOnCounter;
     bool blink;
     byte blinktime;
     const byte BLINKTIME=5;
     string[] Day_Week = new string[] { " Off"," Sun"," Mon"," Tues"," Wednes"," Thurs"," Fri"," Satur"," Daily"," Month" };
     //char* Day[] = {"o","t"};
        

       //  struct tm newtm = new  struct tm ;
    /*
   Day is 0 to 8 0-Daily Program,1-Sun,2-Mon,3-Tues,4-Wednes,5-Thurs,6-Frid,7-Satur,8-Off
   */
 struct Program
   {
     public byte Day;
     public byte OnHour;
     public byte OnMin;
     public byte OffHour;
     public byte OffMin;
     public byte DayOff;
     public byte date;
     public byte month;
   };

    //Eight Program Point 
   Program []  ProgramPoint = new Program[8];
               

        public Form1()
        {
            InitializeComponent();
            timer1.Interval = 10;
            timer1.Enabled = true;

            timer2.Interval = 100;
            timer2.Enabled = true;

            timer3.Interval = 2000;
            timer3.Enabled = true;
            timer3.Stop();

            textBox1.TextAlign = HorizontalAlignment.Center;
            label1.Text = " ";
            label1.BackColor = Color.Red;
            label1.Visible = false;
            InitProgramPoint();
            if (Status == 0) textBox2.Text = "Manu";
            TimerDisplayOnCounter = TimerDisplayOn;
            DisplayOn = true;
          
            
        }

    


        
        

        void InitProgramPoint()
        {
             //All ProgramPoint is Off - default 
            for (byte i = 0; i < 7; i++)
            {
                ProgramPoint[i].Day = 0; 
                ProgramPoint[i].OnHour = 0;
                ProgramPoint[i].OnMin = 0;
                ProgramPoint[i].DayOff = 1;
                ProgramPoint[i].OffHour = 0;
                ProgramPoint[i].OffMin = 0;
            }

            //For test
            ProgramPoint[0].Day=8;   //0  off, 8 for Daily 
            ProgramPoint[0].OnHour =18;
            ProgramPoint[0].OnMin = 30;
            ProgramPoint[0].OffHour =18;
            ProgramPoint[0].OffMin =44;

            ProgramPoint[1].Day = 7;   //0  off, 8 for Daily 
            ProgramPoint[1].OnHour = 10;
            ProgramPoint[1].OnMin = 37;
            ProgramPoint[1].OffHour = 10;
            ProgramPoint[1].OffMin = 40;







            /*
            DateTime dt = DateTime.Now;
            DateTime don = new DateTime(dt.Year, dt.Month, dt.Day, 22, 0, 0);
            DateTime doff = new DateTime (dt.Year, dt.Month, dt.Day+1, 0, 5, 0);
            long lton = don.Ticks;
            long ltoff = doff.Ticks;
            Console.Write(lton.ToString());
            Console.Write(ltoff.ToString());
           // Console.ReadKey();
             * */
        }





       

        private void timer1_Tick(object sender, EventArgs e)
        {
            Display();

            if (Power!=8) label1.Visible = true;
             else label1.Visible = false;
           
          

            if (button == 3)
            {
                Power = 9;
                Status = 0;
            }

            if (button == 2)
            {
                Power = 8;
                Status = 0;
            }

            if (button == 1) Menu();




        }



        void Display()
        {
            timer1.Enabled = false;
            textBox1.Text  = string.Format("{0:HH:mm:ss tt}", DateTime.Now);
            if (Status == 0) textBox2.Text = "M";   
            else if ( Status == 1 ) textBox2.Text = "A";
            if (Power != 9 && Power != 8)
            {
                if (blink) textBox2.Text = textBox2.Text + "P" + Power.ToString();
                else textBox2.Text = textBox2.Text = "A";
            }
            if (DisplayOn)
            {
                textBox1.BackColor = Color.LightGreen;
                textBox2.BackColor = Color.LightGreen;
            }
            else
            {
                textBox1.BackColor = Color.DarkGreen;
                textBox2.BackColor = Color.DarkGreen;
            }

            CheckProgramPoint();
            timer1.Enabled = true;
        }



        void CheckProgramPoint()
        {
             if (Status == 0) return;
             DateTime time = DateTime.Now ;
             int timenow = (int)time.Hour * 60 + (int)time.Minute;
             byte DayToday=(byte)(time.DayOfWeek +1) ;    //Callculate from date month and years 
             byte dayoff = DayToday;
             Power = 8; // By Default Power is off 
            
           for (byte i = 0; i < 7; i++)
            {

                if (ProgramPoint[i].Day == 0) continue; //Point is off

                   //for Daily allarm 
                if (ProgramPoint[i].Day == 8) //Point is Daily Mode
                {
               
                    int timeon = ProgramPoint[i].OnHour * 60 + ProgramPoint[i].OnMin;
                    int timeoff= ProgramPoint[i].OffHour * 60 + ProgramPoint[i].OffMin;
                    if (timeoff > timeon)  // Time off is today
                    {
                        if ((timenow >= timeon) && (timenow < timeoff))
                        {
                            Power = i;
                            return;
                        }

                    }
                    else                 // Time off is next day
                    {
                        if (timenow >= timeon || timenow < timeoff)
                        {
                            Power = i;
                            return;
                        }
                    }
                }

                // for Day of the Week Allarm 
                if (ProgramPoint[i].Day != 8) //Point is Weekly Mode 
                {

                    if (ProgramPoint[i].Day == DayToday)   // Check for On 
                    {
                        int timeon = ProgramPoint[i].OnHour * 60 + ProgramPoint[i].OnMin;
                        int timeoff = ProgramPoint[i].OffHour * 60 + ProgramPoint[i].OffMin;
                        if (timeoff > timeon)  // Time off is today
                        {
                            if ((timenow >= timeon) && (timenow < timeoff))
                            {
                                Power = i;
                                return;
                            }

                        }
                        else                 // Time off is next day
                        {
                            if (timenow >= timeon || timenow < timeoff)
                            {
                                Power = i;
                                return;
                            }
                        }
                        continue;
                    }


                    if (ProgramPoint[i].DayOff == DayToday) //Check for off
                    {
                        int timeoff = ProgramPoint[i].OffHour * 60 + ProgramPoint[i].OffMin;
                        if (timenow < timeoff)
                        {
                            Power = i; //On
                            return;
                        }
                        continue;
                    }

                }
 

                    // for Monthly Allarm
                if (ProgramPoint[i].Day == 9)//Point is Monthly Mode
                {
                }
                         
          

               }

         }



/*
         void CheckProgramPoint()
        {
            if (Status == 0) return;
             DateTime time = DateTime.Now;

            if (Power == 8)  //Check for ON
            {
                for (byte i = 0; i < 7; i++)
                {
                    if (ProgramPoint[i].Day == 8) //Daily
                    {
                      if(CompareTime(ProgramPoint [i].OnHour ,ProgramPoint [i].OnMin))
                      {
                          Power = i;
                          break;
                      }
                    }

                    if (ProgramPoint[i].Day == (byte)time.DayOfWeek)  //Day of Week
                     {
                         if(CompareTime(ProgramPoint [i].OnHour ,ProgramPoint [i].OnMin))
                      {
                          Power=i;
                          break;
                      }

                    }


                }

            }
            else  //Check for Off
            {
                for (byte i = 0; i < 7; i++)
                {
                    if (ProgramPoint[i].Day == 8) //Daily
                    {
                      if(!CompareTime(ProgramPoint [i].OffHour ,ProgramPoint [i].OffMin))
                      {
                          Power=8;
                          break;
                      }
                    }

                    if (ProgramPoint[i].DayOff == (byte)time.DayOfWeek)  //Day of Week
                     {
                         if(!CompareTime(ProgramPoint [i].OffHour ,ProgramPoint [i].OffMin))
                      {
                          Power=8;
                          break;
                      }

                    }


                }













            }


        }

*/
         bool CompareTime (byte hour, byte minute)
         {
             DateTime time = DateTime.Now;
             if (hour >= time.Hour)
             {
                 if (minute >= time.Minute) return true;
             }
             return false;
         }

        void Menu()
        {
            textBox2.Text = "Auto/Manu";
            if (Status == 1) textBox1.Text = "Auto";
            else textBox1.Text = "Manu";
            do
            {
              PressKey();
               if (button == 2)
                {
                   button = 0;
                  return;
                }

               if (button == 3)
               {
                   if (Status == 1) Status = 0;
                   else Status = 1;
               }

                if (Status == 1) textBox1.Text = "Auto";
                else textBox1.Text = "Manu";
            
             } while( button!=1 && button!=0);

                 if (button == 0) return;

              ProgramMenu();
              //Next Menu



            return;


           
        }

        void ProgramMenu ()
        {
            byte program_number=0;


             do  
            {
                //Enter day of the week 0- to daily 8-to off no program active 
                textBox2.Text = "P" + program_number.ToString() + Day_Week[ProgramPoint[program_number].Day];
                textBox1.Text = Day_Week[ProgramPoint[program_number].Day];// ProgramPoint[program_number].Day.ToString();
                 do
                 {
                    PressKey();
                    if (button == 3)  // Plus
                    {
                        ProgramPoint[program_number].Day++;
                        if (ProgramPoint[program_number].Day >= 9) ProgramPoint[program_number].Day = 1;
                        textBox1.Text = Day_Week[ProgramPoint[program_number].Day];
                        
                    }

                    if (button == 2) // Minus  Return Menu 
                      {  
                         button=0;
                          return;
                      }  

                    if (button == 0) break; // No Key Press                                          
                 } while (button != 1 && button != 0) ;

                  // Test is programpoint not Off     

                if (ProgramPoint[program_number].Day != 0)
                {
                    // if Not Next Settup Program On time Hours 

                    textBox2.Text = "P" + program_number.ToString() + " OnHour";
                    textBox1.Text = ProgramPoint[program_number].OnHour.ToString() + ":" + ProgramPoint[program_number].OnMin.ToString();
                    do
                    {
                        ProgramPoint[program_number].OnHour = Adj(ProgramPoint[program_number].OnHour, 0, 23);
                        textBox1.Text = ProgramPoint[program_number].OnHour.ToString() + ":" + ProgramPoint[program_number].OnMin.ToString();
                    } while (button != 1 && button != 0);
                    if (button == 0) break; // No Key Press

                    // Next Settup Program On time Minute
                    textBox2.Text = "P" + program_number.ToString() + " OnMin";
                    textBox1.Text = ProgramPoint[program_number].OnHour.ToString() + ":" + ProgramPoint[program_number].OnMin.ToString();
                    do
                    {
                        ProgramPoint[program_number].OnMin = Adj(ProgramPoint[program_number].OnMin, 0, 59);
                        textBox1.Text = ProgramPoint[program_number].OnHour.ToString() + ":" + ProgramPoint[program_number].OnMin.ToString();
                    } while (button != 1 && button != 0); 
                    if (button == 0) break; // No Key Press


                    // Next Settup Program Off time Hour

                    textBox2.Text = "P" + program_number.ToString() + " OffHour";
                    textBox1.Text = ProgramPoint[program_number].OffHour.ToString() + ":" + ProgramPoint[program_number].OffMin.ToString();
                    do
                    {
                        ProgramPoint[program_number].OffHour = Adj(ProgramPoint[program_number].OffHour, 0, 23);
                        textBox1.Text = ProgramPoint[program_number].OffHour.ToString() + ":" + ProgramPoint[program_number].OffMin.ToString();
                    } while (button != 1 && button != 0);
                    if (button == 0) break; // No Key Press

                    // Next Settup Program Off time Minute
                    textBox2.Text = "P" + program_number.ToString() + " OffMin";
                    textBox1.Text = ProgramPoint[program_number].OffHour.ToString() + ":" + ProgramPoint[program_number].OffMin.ToString();
                    do
                    {
                        ProgramPoint[program_number].OffMin = Adj(ProgramPoint[program_number].OffMin, 0, 59);
                        textBox1.Text = ProgramPoint[program_number].OffHour.ToString() + ":" + ProgramPoint[program_number].OffMin.ToString();
                    } while (button != 1 && button != 0);
                    if (button == 0) break; // No Key Press
                       // On need day_off corection
                    int timeon = ProgramPoint[program_number].OnHour * 60 + ProgramPoint[program_number].OnMin;
                    int timeoff = ProgramPoint[program_number].OffHour * 60 + ProgramPoint[program_number].OffMin;
                    ProgramPoint[program_number].DayOff = ProgramPoint[program_number].Day;
                    if (timeoff < timeon)
                    {
                        ProgramPoint[program_number].DayOff++;   
                        if (ProgramPoint[program_number].DayOff == 8) ProgramPoint[program_number].DayOff =1;
                    }
                   

                } 
                 
                program_number++;

            } while ((program_number < 8) && (button != 0));

             if (button == 0)   //Exit whit time out 
             {
                 int timeon = ProgramPoint[program_number].OnHour * 60 + ProgramPoint[program_number].OnMin;
                 int timeoff = ProgramPoint[program_number].OffHour * 60 + ProgramPoint[program_number].OffMin;
                 ProgramPoint[program_number].DayOff = ProgramPoint[program_number].Day;
                 if (timeoff < timeon)
                 {
                     ProgramPoint[program_number].DayOff++;
                     if (ProgramPoint[program_number].DayOff == 8) ProgramPoint[program_number].DayOff = 1;
                 }
                
             }

        }


        private void ClockMenu()
        {
            textBox1.Text = "Set Clock";
             do 
             {
                 PressKey();
             } while (button != 1 && button != 0);

             button = 0;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            button = 3; // + On
            if (!DisplayOn) button = 0; 
            DisplayOn = true;
            TimerDisplayOnCounter = TimerDisplayOn;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            button = 2; // - Off
            if (!DisplayOn) button = 0; 
            DisplayOn = true;
            TimerDisplayOnCounter = TimerDisplayOn;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button = 1; //Ok Menu
            if (!DisplayOn) button = 0; 
            DisplayOn = true;
            TimerDisplayOnCounter = TimerDisplayOn;
           // timer3.Start();
            button2.MouseDown += (s, e1) =>
            {
               timer3.Start();
                 
            };

             button2.MouseUp += (s, e1) =>
            {
               
                timer3.Stop();

            };
           
           
        }
         

     

        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
           MessageBox.Show ("I am here !");

        }
        



        private void timer2_Tick(object sender, EventArgs e)
        {
            timer2.Enabled = false;
            TimerDisplayOnCounter--;
            if (TimerDisplayOnCounter == 0) DisplayOn = false;
            delay++;
            delay1++;
            blinktime--;
            if (blinktime == 255)
            {
                if (blink) blink = false;
                else blink = true;
                blinktime = BLINKTIME;
            }
           timer2.Enabled = true;

        }

        private void button4_Click(object sender, EventArgs e)
        {

            this.Close();
        }

      
         void  PressKey ()
         {
            button = 0;
            delay = 0;
            timer1.Stop();
            do
            {
              Application.DoEvents();
            }
            while ((button == 0) && (delay < 100));
            timer1.Start();
         }


          byte Adj(byte begin, byte min,byte max)
         {
             byte adj;
             adj=begin;
              PressKey();
              if (button == 3)
              {
                  adj++;
                  if (adj >= max) adj = min;
              }

              if (button == 2)
              {
                  adj--;
                  if (adj == 255) adj = max;
              }
             return adj;
         }

          private void timer3_Tick(object sender, EventArgs e)
          {
              timer3.Stop();
              ClockMenu();
          }


    }
}
